/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.internal.parser;

import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.PatternRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;

/**
 * Scanner for the scenario sections of a scenario file
 * 
 * @author Doug Satchwell
 */
public class ExamplesScanner extends RuleBasedScanner {

	public ExamplesScanner(KeyWords keywords) {
		EndOfLineRule commentRule = new EndOfLineRule(keywords.comment(), JFunctionToken.COMMENT);
		
		PatternRule examplesRule = new PatternRule(keywords.examples(),":",JFunctionToken.EXAMPLES,(char)0,false);
		examplesRule.setColumnConstraint(0);
		
		SingleLineRule pipeRule = new SingleLineRule("|","|",JFunctionToken.EXAMPLE_FIELD,'\\',true,true){
			@Override
			protected boolean endSequenceDetected(ICharacterScanner scanner) {
				boolean found = super.endSequenceDetected(scanner);
				if (found) {
					// unread the last character if it was a pipe..
					scanner.unread();
					if ('|' == scanner.read()) {
						scanner.unread();
					}
				}
				return found;
			}
		};
		
		IRule[] rules = new IRule[] {
			commentRule, 
			examplesRule,
			pipeRule
		};
		setRules(rules);
	}
}
